﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Drawing;
using DockSample.DAO;

namespace DockSample
{
    class OriginalDiagramDAO : BaseDAO
    {
        private NonFunctionRequirmentDAO nfrDAO = new NonFunctionRequirmentDAO();
        public bool AddDiagram(OriginalDiagram diagram)
        {


            try
            {
       //         this.DeleteDiagram(diagram);
                //   this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "insert into OriginalDiagram( id ,x ,y,width,height,name,isSelected,newModelFormId,treeNode,diagramtype,assocInforModelFormId,assocStateModelFormId) "
                   + " values (@id,@x,@y,@width,@height,@name,@isSelected,@newModelFormId,@treeNode,@diagramtype,@assocInforModelFormId,@assocStateModelFormId)";
                cmd.Parameters.Add(new SQLiteParameter("id", diagram.Id));
                cmd.Parameters.Add(new SQLiteParameter("x", diagram.UpLeftpoint.X));
                cmd.Parameters.Add(new SQLiteParameter("y", diagram.UpLeftpoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("width", diagram.Width));
                cmd.Parameters.Add(new SQLiteParameter("height", diagram.Height));
                cmd.Parameters.Add(new SQLiteParameter("name", diagram.Name));
                cmd.Parameters.Add(new SQLiteParameter("isSelected", false));



                if ("DockSample.HighOrderObjectDiagram".Equals(this.GetSimpleType(diagram.GetType().ToString())))
                {
                    if (((HighOrderObjectDiagram)diagram).HasModelForm)
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", ((HighOrderObjectDiagram)diagram).NewModelForm.id));
                    }
                    else
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                    }
                }
                else if ("DockSample.HighStateDiagram".Equals(this.GetSimpleType(diagram.GetType().ToString())))
                {
                    if (((HighStateDiagram)diagram).HasModelForm)
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", ((HighStateDiagram)diagram).NewModelForm.id));
                    }
                    else
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                    }
                }
                else if ("DockSample.InformationSourceDiagram".Equals(this.GetSimpleType(diagram.GetType().ToString())))
                {
                    if (((InformationSourceDiagram)diagram).HasModelForm)
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", ((InformationSourceDiagram)diagram).NewModelForm.id));
                    }
                    else
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                    }
                }


                else
                {
                    cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                }


                if (diagram.treeNode != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("treeNode", diagram.treeNode.FullPath));
                }
                else
                {
                    cmd.Parameters.Add(new SQLiteParameter("treeNode", null));
                }


                cmd.Parameters.Add(new SQLiteParameter("diagramtype", diagram.GetType().ToString()));

                if (diagram is ObjectDiagram)
                {
                    //wjw++
                    cmd.Parameters.Add(new SQLiteParameter("assocInforModelFormId", ((ObjectDiagram)diagram).assocInforModelFormId));
                    cmd.Parameters.Add(new SQLiteParameter("assocStateModelFormId", ((ObjectDiagram)diagram).assocStateModelFormId));
                }
                else
                {
                    cmd.Parameters.Add(new SQLiteParameter("assocInforModelFormId", null));
                    cmd.Parameters.Add(new SQLiteParameter("assocStateModelFormId", null));
                }
                //向数据库添加Method和Attribute
                if (diagram is ObjectDiagram)
                {
                    MethodDAO mdbo = new MethodDAO();
                    AttributeDAO adbo = new AttributeDAO();
                    foreach (Method m in ((ObjectDiagram)diagram).MethodList)
                    {
                        mdbo.AddMethod(m, diagram.Id);

                    }

                    foreach (Attribute attr in ((ObjectDiagram)diagram).AttributeList)
                    {
                        adbo.AddAttribute(attr, diagram.Id);
                    }
                }


                cmd.ExecuteNonQuery();
                this.saveNFR(diagram);
               
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                CloseConnection();
            }


        }
        public bool DeleteDiagram(OriginalDiagram diagram)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from OriginalDiagram where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", diagram.Id));
                //同时删除关联的Method和Attr
    //            Console.WriteLine("diagram is" + diagram.GetType());
                if (diagram is ObjectDiagram)
                {
                    Console.WriteLine("DELETE OK");
                    MethodDAO mdbo = new MethodDAO();
                    AttributeDAO adbo = new AttributeDAO();
                    foreach (Method m in ((ObjectDiagram)diagram).MethodList)
                    {
                        mdbo.DeleteMethod(m);

                    }

                    foreach (Attribute attr in ((ObjectDiagram)diagram).AttributeList)
                    {
                        adbo.DeleteAttribute(attr);
                    }
                }

                cmd.ExecuteNonQuery();

                this.deleteRelations(diagram);
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                CloseConnection();
            }
        }

        public OriginalDiagram GetDiagramById(String id, DummyModelForm form)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from OriginalDiagram where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    //  OriginalDiagram diagram=null;

                    String diagramType = reader["diagramType"].ToString();
                    String name = reader["name"].ToString();

                    int x = int.Parse(reader["x"].ToString());
                    int y = int.Parse(reader["y"].ToString());
                    int width = int.Parse(reader["width"].ToString());
                    int height = int.Parse(reader["height"].ToString());
                    string newModelFormId = reader["newModelFormId"].ToString();
                    string AssocInforModelFormId = reader["assocInforModelFormId"].ToString();
                    string AssocStateModelFormId = reader["assocStateModelFormId"].ToString();
                    
                    //1
                    if ("DockSample.HighOrderObjectDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        //  Console.WriteLine("22222222");
                        HighOrderObjectDiagram diagram = new HighOrderObjectDiagram(
                        new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Id = id;
                        diagram.Name = name;
                        
                        //添加方法
                        MethodDAO mdbo = new MethodDAO();
                        diagram.MethodList = mdbo.GetMethodList(diagram);

                        //      Console.WriteLine("!!!!methodlist length is " + mdbo.GetMethodList(diagram).Count);

                        //添加属性
                        AttributeDAO adbo = new AttributeDAO();
                        diagram.AttributeList = adbo.GetAttributeList(diagram);
                        //添加newModelFormId
                        diagram.newModelFormId = newModelFormId;
                        //wjw++ 相关联的Infor和State的Form的ID
                        diagram.assocInforModelFormId = AssocInforModelFormId;
                        diagram.assocStateModelFormId = AssocStateModelFormId;
                        //添加NFR
                        diagram.ObjectNFRList = nfrDAO.GetNFRByObjId(diagram.id);
                        //wjw++从数据库取出Diagram时，全部不选中
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                    //2
                    else if ("DockSample.HighStateDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        HighStateDiagram diagram = new HighStateDiagram(
                         new Point(x + width / 2, y + height / 2), width, height);

                        diagram.Name = name;
                        diagram.Id = id;

                        //添加newModelFormId
                        diagram.newModelFormId = newModelFormId;
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                    //3
                    else if ("DockSample.InformationSourceDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        InformationSourceDiagram diagram = new InformationSourceDiagram(
                        new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Name = name;
                        diagram.Id = id;

                        //添加newModelFormId
                        diagram.newModelFormId = newModelFormId;
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                   
                    //4
                    else if ("DockSample.PrimitiveObjectDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        PrimitiveObjectDiagram diagram = new PrimitiveObjectDiagram(
                         new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Name = name;
                        diagram.Id = id;

                        //添加方法
                        MethodDAO mdbo = new MethodDAO();
                        diagram.MethodList = mdbo.GetMethodList(diagram);

                        //添加属性
                        AttributeDAO adbo = new AttributeDAO();
                        diagram.AttributeList = adbo.GetAttributeList(diagram);

                        //添加NFR
                        diagram.ObjectNFRList = nfrDAO.GetNFRByObjId(diagram.id);
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                    //5
                    else if ("DockSample.PrimitiveStateDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        PrimitiveStateDiagram diagram = new PrimitiveStateDiagram(
                         new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Name = name;

                        diagram.Id = id;
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                    //6
                    else if ("DockSample.ProcessDiagram".Equals(this.GetSimpleType(diagramType)))
                    {

                        ProcessDiagram diagram = new ProcessDiagram(
                            new Point(x + width / 2, y + height / 2), width, height);

                        diagram.Name = name;
                        diagram.Id = id;
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                    //wjw++
                    //7
                    else if ("DockSample.ExternalInterfaceIn".Equals(this.GetSimpleType(diagramType)))
                    {

                        ExternalInterfaceIn diagram = new ExternalInterfaceIn(new Point(x + width / 2, y + height / 2), width, height);

                        diagram.Name = name;
                        diagram.Id = id;
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                    //8
                    else if ("DockSample.ExternalInterfaceOut".Equals(this.GetSimpleType(diagramType)))
                    {

                        ExternalInterfaceOut diagram = new ExternalInterfaceOut(new Point(x + width / 2, y + height / 2), width, height);

                        diagram.Name = name;
                        diagram.Id = id;
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                    //9
                    else if ("DockSample.DataStore".Equals(this.GetSimpleType(diagramType)))
                    {

                        DataStore diagram = new DataStore(new Point(x + width / 2, y + height / 2), width, height);

                        diagram.Name = name;
                        diagram.Id = id;
                        diagram.IsSelected = false;
                        diagram.ModelForm = form;
                        return diagram;
                    }
                   

                    else
                    {
                        return null;
                    }

                }
                else
                {
                    return null;
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                CloseConnection();
            }
        }

        private void deleteRelations(OriginalDiagram od)
        {
            this.deleteNFR(od);

            deleteRelatedLine(od);
        }

        private void deleteRelatedLine(OriginalDiagram od)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete  from OriginalLine where startDiagramId=@startDiagramId";
                cmd.Parameters.Add(new SQLiteParameter("startDiagramId", od.id));
                cmd.ExecuteNonQuery();
                SQLiteCommand cmd1 = this.GetCmd();
                cmd1.CommandText = "delete  from OriginalLine where endDiagramId=@endDiagramId";
                cmd1.Parameters.Add(new SQLiteParameter("endDiagramId", od.id));
                cmd1.ExecuteNonQuery();
            }
            catch (System.Exception ex)
            {

                throw ex;
              
            }
            finally
            {
                this.conn.Close();
            }
        }

        private void deleteNFR(OriginalDiagram od)
        {
            if (od is ObjectDiagram)
            {
                ObjectDiagram d=(ObjectDiagram)od;
                foreach (NonFunctionRequirment nfr in d.ObjectNFRList)
                {
                    nfrDAO.DeleteNonFunctionRequirment(nfr);
                }
            }
        }

        private void saveNFR(OriginalDiagram od)
        {
            if (od is ObjectDiagram)
            {
                ObjectDiagram d = (ObjectDiagram)od;
                foreach (NonFunctionRequirment nfr in d.ObjectNFRList)
                {
                    nfrDAO.AddNonFunctionRequirment(nfr, d.id);
                }
            }
        }

        
    }
}
